<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>供应商列表</title>
    <link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../../css/style.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        /* 复制material_list.html的样式 */
        .submenu { display: none; }
        .menu-item.active > .submenu { display: block; }
        .menu-arrow { transition: transform 0.2s; }
        .menu-item.active > a > .menu-arrow { transform: rotate(90deg); }
        .table th { background-color: #f8f9fa; }
        .submenu li a.active { color: #28a745; font-weight: bold; transform: translateY(2px); display: inline-block; }
        .search-area { background: #f8f9fa; padding: 10px; border-radius: 8px; margin: 10px 0; width: 100%; }
        .search-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
        .search-item { flex: 0 0 auto; margin-right: 10px; margin-bottom: 5px; }
        .search-item .form-control, .search-item .form-select { width: 120px; height: 31px; font-size: 14px; }
        .search-item .btn { padding: 4px 8px; }
        .search-label { display: block; margin-bottom: 5px; font-size: 14px; color: #555; white-space: nowrap; }
        .table-responsive { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
        .custom-table { width: 100%; border-collapse: separate; border-spacing: 0; }
        .custom-table th { background: #f8f9fa; color: #495057; font-weight: 600; padding: 12px 15px; border-bottom: 2px solid #dee2e6; white-space: nowrap; }
        .custom-table td { padding: 12px 15px; border-bottom: 1px solid #eee; vertical-align: middle; }
        .action-buttons { display: flex; gap: 8px; }
        .action-buttons .btn { 
            padding: 4px 10px; 
            font-size: 13px;
            min-width: 60px;
            white-space: nowrap;
        }
        .page-title { font-size: 22px; font-weight: 600; margin-bottom: 20px; color: #333; display: flex; align-items: center; gap: 10px; }
        .page-title-icon { font-size: 24px; }
        .ms-auto { margin-left: auto; }
        .add-button { padding: 8px 16px; display: flex; align-items: center; gap: 5px; font-weight: 500; }
        .content-wrapper { padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin: 20px; }
        
        /* 添加按钮组样式 */
        .action-button-group {
            display: flex;
            gap: 15px;
            align-items: center;
            justify-content: flex-end;
        }
        
        .action-button-group .add-button {
            padding: 8px 16px;
            display: flex;
            align-items: center;
            gap: 8px;
            font-weight: 500;
            min-width: 140px;
            justify-content: center;
        }
        
        /* iOS风格模态框样式 */
        .modal.ios-style .modal-content {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            border: none;
            overflow: hidden;
        }
        
        .modal.ios-style .modal-header {
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
            padding: 20px 20px 15px;
        }
        
        .modal.ios-style .modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }
        
        .modal.ios-style .modal-body {
            padding: 20px;
        }
        
        .modal.ios-style .modal-footer {
            border-top: 1px solid rgba(0, 0, 0, 0.1);
            padding: 15px 20px;
        }
        
        .modal.ios-style .btn-close {
            opacity: 0.7;
            transition: opacity 0.2s;
        }
        
        .modal.ios-style .btn-close:hover {
            opacity: 1;
        }
        
        .modal.ios-style .btn {
            border-radius: 12px;
            font-weight: 500;
            padding: 8px 20px;
            transition: all 0.2s;
        }
        
        .modal.ios-style .btn-primary {
            background: #007AFF;
            border-color: #007AFF;
        }
        
        .modal.ios-style .btn-primary:hover {
            background: #0056CC;
            border-color: #0056CC;
        }
        
        .modal.ios-style .btn-secondary {
            background: #E1E1E6;
            border-color: #E1E1E6;
            color: #333;
        }
        
        .modal.ios-style .btn-secondary:hover {
            background: #D1D1D6;
            border-color: #D1D1D6;
        }
        
        /* 改进详情模态框样式 */
        .modal.ios-style .modal-dialog.modal-lg-custom {
            max-width: 900px;
            width: 80%;
        }
        
        .modal.ios-style .modal-dialog.modal-xl-custom {
            max-width: 1300px;
            width: 90%;
        }
        
        /* 修复模态框滚动问题 */
        .modal-dialog {
            max-height: 100%;
            display: flex;
            flex-direction: column;
        }
        
        .modal-content {
            max-height: 90vh;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .modal-body {
            overflow-y: auto;
            max-height: calc(90vh - 120px);
            padding: 15px;
        }
        
        .table-responsive {
            overflow-y: auto;
            max-height: 60vh;
        }
        
        /* 表格样式 */
        .records-table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            margin-bottom: 0;
        }
        
        .records-table th {
            background: #f8f9fa;
            color: #333;
            font-weight: 600;
            padding: 12px 15px;
            border-bottom: 2px solid #dee2e6;
            white-space: nowrap;
        }
        
        /* 表格标题固定 */
        .records-table thead th {
            position: sticky;
            top: 0;
            background-color: #f8f9fa;
            z-index: 10;
            box-shadow: 0 1px 2px rgba(0,0,0,0.1);
        }
        
        /* 表格单元格样式 */
        .records-table td {
            max-width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            vertical-align: middle;
            padding: 10px 15px;
        }
        
        .records-table td.description-cell {
            white-space: normal;
            max-height: 80px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
        }
        
        /* 可点击行样式 */
        .records-table tbody tr {
            cursor: pointer;
            transition: background-color 0.2s;
        }
        
        .records-table tbody tr:hover {
            background-color: rgba(0, 123, 255, 0.05);
        }
        
        /* 详情卡片样式 */
        .detail-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 15px;
        }
        
        .detail-item {
            display: flex;
            flex-direction: column;
            margin-bottom: 5px;
        }
        
        .detail-label {
            font-size: 14px;
            color: #6c757d;
            margin-bottom: 5px;
        }
        
        .detail-value {
            font-size: 15px;
            color: #333;
            font-weight: 500;
        }
        
        .detail-value.description {
            white-space: pre-line;
            line-height: 1.5;
        }
        
        /* 状态标签样式 */
        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
            text-align: center;
            min-width: 80px;
        }
        
        .status-pending {
            background-color: #fff3cd;
            color: #856404;
        }
        
        .status-approved {
            background-color: #d4edda;
            color: #155724;
        }
        
        .status-rejected {
            background-color: #f8d7da;
            color: #721c24;
        }
        
        /* iOS风格模态框样式 */
        .ios-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.4);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1050;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .ios-modal.show {
            display: flex;
            opacity: 1;
        }

        .ios-modal-content {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 0;
            max-width: 400px;
            width: 90%;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            transform: scale(0.8);
            transition: transform 0.3s ease;
        }

        .ios-modal.show .ios-modal-content {
            transform: scale(1);
        }

        .ios-modal-header {
            padding: 20px 20px 10px;
            text-align: center;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }

        .ios-modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .ios-modal-body {
            padding: 20px;
            max-height: 60vh;
            overflow-y: auto;
        }

        .ios-detail-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 0;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
        }

        .ios-detail-item:last-child {
            border-bottom: none;
        }

        .ios-detail-label {
            font-weight: 500;
            color: #666;
            font-size: 14px;
        }

        .ios-detail-value {
            font-weight: 400;
            color: #333;
            font-size: 14px;
            text-align: right;
            max-width: 60%;
            word-wrap: break-word;
        }

        .ios-modal-footer {
            padding: 15px 20px 20px;
            text-align: center;
        }

        .ios-close-btn {
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 12px;
            padding: 12px 30px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.2s ease;
        }

        .ios-close-btn:hover {
            background: #0056CC;
        }

        /* 滚动条样式 */
        .ios-modal-body::-webkit-scrollbar,
        .modal.ios-style .modal-body::-webkit-scrollbar {
            width: 4px;
        }

        .ios-modal-body::-webkit-scrollbar-track,
        .modal.ios-style .modal-body::-webkit-scrollbar-track {
            background: transparent;
        }

        .ios-modal-body::-webkit-scrollbar-thumb,
        .modal.ios-style .modal-body::-webkit-scrollbar-thumb {
            background: rgba(0, 0, 0, 0.2);
            border-radius: 2px;
        }
        
        /* 卡片样式优化 */
        .modal.ios-style .card {
            border-radius: 15px;
            overflow: hidden;
            border: none;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        }
        
        .modal.ios-style .card-header {
            background-color: rgba(248, 249, 250, 0.8);
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            padding: 12px 16px;
        }
        
        .modal.ios-style .card-body {
            padding: 16px;
        }
        
        /* 表单元素样式 */
        .modal.ios-style .form-control,
        .modal.ios-style .form-select {
            border-radius: 10px;
            border: 1px solid rgba(0, 0, 0, 0.1);
            padding: 10px 15px;
            transition: all 0.2s;
        }
        
        .modal.ios-style .form-control:focus,
        .modal.ios-style .form-select:focus {
            border-color: #007AFF;
            box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
        }
        
        /* 增强模态框背景效果 */
        .modal-backdrop.show {
            opacity: 0.2;
            backdrop-filter: blur(8px);
            -webkit-backdrop-filter: blur(8px);
        }
        
        /* 优化按钮样式 */
        .btn {
            border-radius: 8px;
            font-weight: 500;
            transition: all 0.2s ease;
        }
        
        .btn-sm {
            padding: 0.25rem 0.5rem;
            border-radius: 6px;
        }
        
        .btn-outline-primary {
            border-color: #007AFF;
            color: #007AFF;
        }
        
        .btn-outline-primary:hover {
            background-color: #007AFF;
            color: white;
        }
        
        .btn-outline-secondary {
            border-color: #8E8E93;
            color: #8E8E93;
        }
        
        .btn-outline-secondary:hover {
            background-color: #8E8E93;
            color: white;
        }
        
        /* 增强模态框动画效果 */
        .modal.fade .modal-dialog {
            transition: transform 0.3s ease-out, opacity 0.3s ease;
            transform: scale(0.95);
            opacity: 0;
        }
        
        .modal.show .modal-dialog {
            transform: scale(1);
            opacity: 1;
        }
        
        /* 美化表单控件 */
        .form-control, .form-select {
            transition: all 0.2s ease;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: #007AFF;
            box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
        }
        
        /* 优化卡片阴影和边角 */
        .card {
            border-radius: 12px;
            border: none;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            overflow: hidden;
        }
        
        /* 优化状态标签样式 */
        .status-badge {
            border-radius: 6px;
            font-weight: 600;
            padding: 4px 10px;
        }
        
        /* 表格行悬浮效果增强 */
        .records-table tbody tr:hover {
            background-color: rgba(0, 122, 255, 0.08);
        }
        
        /* 表格内按钮样式优化 */
        .records-table .btn {
            padding: 4px 12px;
            font-size: 12px;
            border-radius: 6px;
        }
    </style>
</head>
<body>
    <div class="admin-layout">
        <!-- 顶部导航栏 -->
        <header class="admin-header">
            <div class="header-left">
                <div class="logo">生产过程管理系统</div>
            </div>
            <div class="header-right">
                <span class="user-info">管理员</span>
                <span class="user-name">
                    <script>
                        try {
                            const userInfo = localStorage.getItem('userInfo');
                            const user = JSON.parse(userInfo);
                            document.write(user.name || '未登录');
                        } catch (e) {
                            document.write('未登录');
                        }
                    </script>
                </span>
                <button class="logout-btn" onclick="logout()">退出</button>
            </div>
        </header>
        <!-- 左侧菜单 -->
        <aside class="admin-sidebar">
            <ul class="menu-list">
                <li class="menu-item">
                    <a href="../homepage/administrator_home.html">
                        <span class="menu-icon">🏠</span>
                        <span class="menu-title">首页</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">👥</span>
                        <span class="menu-title">人员管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="../person/person_list.html">人员列表</a></li>
                        <li><a href="../person/person_edit.html">添加人员</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">📦</span>
                        <span class="menu-title">物料管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="../material/material_list.html">物料列表</a></li>
                        <li><a href="../material/material_edit.html">添加物料</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">⚙️</span>
                        <span class="menu-title">设备管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="../device/device_list.html">设备列表</a></li>
                        <li><a href="../device/device_edit.html">添加设备</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">📝</span>
                        <span class="menu-title">产品管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="../product/product_list.html">产品列表</a></li>
                        <li><a href="../product/product_edit.html">添加产品</a></li>
                    </ul>
                </li>
                <li class="menu-item active">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">🏢</span>
                        <span class="menu-title">供应商管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="provider_list.html" class="active">供应商列表</a></li>
                        <li><a href="provider_edit.html">添加供应商</a></li>
                    </ul>
                </li>
            </ul>
        </aside>
        <!-- 主内容区域 -->
        <main class="admin-main">
            <div class="content-wrapper">
                <div class="page-title">
                    <span class="page-title-icon">🏢</span>
                    <span>供应商列表</span>
                    <div class="ms-auto action-button-group">
                        <button class="btn btn-primary add-button" onclick="showApplicationRecordsModal()">
                            <i class="fas fa-list"></i> 所有供应商申请
                        </button>
                        <button class="btn btn-warning add-button" onclick="showComplaintRecordsModal()">
                            <i class="fas fa-exclamation-circle"></i> 所有供应商投诉
                        </button>
                        <button class="btn btn-success add-button" onclick="addProvider()">
                            <i class="fas fa-plus"></i> 添加供应商
                        </button>
                    </div>
                </div>
                <!-- 搜索区域 -->
                <div class="search-area">
                    <div class="search-row">
                        <div class="search-item">
                            <label class="search-label">供应商名称</label>
                            <input type="text" class="form-control form-control-sm" id="searchProviderName" placeholder="请输入名称">
                        </div>
                        <div class="search-item">
                            <label class="search-label">企业类型</label>
                            <select class="form-select form-select-sm" id="searchCompanyType">
                                <option value="">全部</option>
                                <option value="国有企业">国有企业</option>
                                <option value="民营企业">民营企业</option>
                                <option value="外资企业">外资企业</option>
                            </select>
                        </div>
                        <div class="search-item">
                            <label class="search-label">经理姓名</label>
                            <input type="text" class="form-control form-control-sm" id="searchManagerName" placeholder="请输入姓名">
                        </div>
                        <div class="search-item">
                            <label class="search-label">供应商评级</label>
                            <select class="form-select form-select-sm" id="searchProviderRating">
                                <option value="">全部</option>
                                <option value="1">1星</option>
                                <option value="2">2星</option>
                                <option value="3">3星</option>
                                <option value="4">4星</option>
                                <option value="5">5星</option>
                            </select>
                        </div>
                        <div class="search-item">
                            <button class="btn btn-sm btn-outline-primary" onclick="searchProviders()">🔍</button>
                        </div>
                        <div class="search-item">
                            <button class="btn btn-sm btn-outline-secondary" onclick="resetSearch()">↺</button>
                        </div>
                    </div>
                </div>
                <!-- 数据表格 -->
                <div class="table-responsive">
                    <table class="table table-hover table-striped custom-table mb-0">
                        <thead>
                            <tr>
                                <th>供应商名称</th>
                                <th>厂址</th>
                                <th>经理姓名</th>
                                <th>经理电话</th>
                                <th>企业类型</th>
                                <th>注册资本</th>
                                <th>员工数量</th>
                                <th>成立年份</th>
                                <th>供应商评级</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="providerTableBody">
                            <!-- 数据将通过JavaScript动态加载 -->
                        </tbody>
                    </table>
                </div>
            </div>
        </main>
    </div>
    <!-- iOS风格供应商详情模态框 -->
    <div class="ios-modal" id="providerDetailModal">
        <div class="ios-modal-content">
            <div class="ios-modal-header">
                <h5 class="ios-modal-title">供应商详情</h5>
            </div>
            <div class="ios-modal-body">
                <div class="ios-detail-item">
                    <span class="ios-detail-label">供应商名称</span>
                    <span class="ios-detail-value" id="detailProviderName">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">厂址</span>
                    <span class="ios-detail-value" id="detailFactoryAddress">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">经理姓名</span>
                    <span class="ios-detail-value" id="detailManagerName">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">经理电话</span>
                    <span class="ios-detail-value" id="detailManagerPhone">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">企业类型</span>
                    <span class="ios-detail-value" id="detailCompanyType">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">注册资本</span>
                    <span class="ios-detail-value" id="detailRegisteredCapital">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">员工数量</span>
                    <span class="ios-detail-value" id="detailEmployeeCount">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">成立年份</span>
                    <span class="ios-detail-value" id="detailEstablishedYear">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">供应商评级</span>
                    <span class="ios-detail-value" id="detailProviderRating">-</span>
                </div>
                <div class="ios-detail-item">
                    <span class="ios-detail-label">企业简介</span>
                    <span class="ios-detail-value" id="detailProfile">-</span>
                </div>
            </div>
            <div class="ios-modal-footer">
                <button class="ios-close-btn" onclick="closeProviderDetailModal()">关闭</button>
            </div>
        </div>
    </div>
    <!-- 供应商详情模态框之后添加以下模态框 -->
    
    <!-- 申请记录模态框 -->
    <div class="modal fade ios-style" id="applicationRecordsModal" tabindex="-1" aria-labelledby="applicationRecordsModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-xl-custom">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="applicationRecordsModalLabel">供应商申请记录</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="card mb-4">
                        <div class="card-header bg-light d-flex justify-content-between align-items-center">
                            <h6 class="mb-0">筛选条件</h6>
                        </div>
                        <div class="card-body">
                            <div class="search-area mb-0">
                                <div class="search-row">
                                    <div class="search-item">
                                        <label class="search-label">供应商名称</label>
                                        <input type="text" class="form-control form-control-sm" id="appSearchProviderName" placeholder="请输入名称">
                                    </div>
                                    <div class="search-item">
                                        <label class="search-label">申请状态</label>
                                        <select class="form-select form-select-sm" id="appSearchStatus">
                                            <option value="">全部</option>
                                            <option value="pending">待审核</option>
                                            <option value="approved">已批准</option>
                                            <option value="rejected">已驳回</option>
                                        </select>
                                    </div>
                                    <div class="search-item">
                                        <button class="btn btn-sm btn-outline-primary" onclick="searchApplications()">
                                            <i class="fas fa-search"></i> 搜索
                                        </button>
                                    </div>
                                    <div class="search-item">
                                        <button class="btn btn-sm btn-outline-secondary" onclick="resetApplicationSearch()">
                                            <i class="fas fa-redo"></i> 重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">申请列表</h6>
                        </div>
                        <div class="card-body p-0" style="max-height: 60vh; overflow: auto;">
                            <table class="table table-hover records-table mb-0">
                                <thead>
                                    <tr>
                                        <th style="width: 20%">供应商名称</th>
                                        <th style="width: 15%">企业类型</th>
                                        <th style="width: 15%">经理姓名</th>
                                        <th style="width: 20%">申请人工号</th>
                                        <th style="width: 10%">状态</th>
                                        <th style="width: 20%">操作</th>
                                    </tr>
                                </thead>
                                <tbody id="applicationRecordsBody">
                                    <!-- 数据将通过JavaScript动态加载 -->
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 投诉记录模态框 -->
    <div class="modal fade ios-style" id="complaintRecordsModal" tabindex="-1" aria-labelledby="complaintRecordsModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-xl-custom">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="complaintRecordsModalLabel">供应商投诉记录</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="card mb-4">
                        <div class="card-header bg-light d-flex justify-content-between align-items-center">
                            <h6 class="mb-0">筛选条件</h6>
                        </div>
                        <div class="card-body">
                            <div class="search-area mb-0">
                                <div class="search-row">
                                    <div class="search-item">
                                        <label class="search-label">供应商名称</label>
                                        <input type="text" class="form-control form-control-sm" id="compSearchProviderName" placeholder="请输入名称">
                                    </div>
                                    <div class="search-item">
                                        <label class="search-label">投诉状态</label>
                                        <select class="form-select form-select-sm" id="compSearchStatus">
                                            <option value="">全部</option>
                                            <option value="pending">待审核</option>
                                            <option value="approved">已批准</option>
                                            <option value="rejected">已驳回</option>
                                        </select>
                                    </div>
                                    <div class="search-item">
                                        <button class="btn btn-sm btn-outline-primary" onclick="searchComplaints()">
                                            <i class="fas fa-search"></i> 搜索
                                        </button>
                                    </div>
                                    <div class="search-item">
                                        <button class="btn btn-sm btn-outline-secondary" onclick="resetComplaintSearch()">
                                            <i class="fas fa-redo"></i> 重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">投诉列表</h6>
                        </div>
                        <div class="card-body p-0" style="max-height: 60vh; overflow: auto;">
                            <table class="table table-hover records-table mb-0">
                                <thead>
                                    <tr>
                                        <th style="width: 15%">供应商名称</th>
                                        <th style="width: 15%">问题材料</th>
                                        <th style="width: 30%">投诉描述</th>
                                        <th style="width: 15%">投诉人工号</th>
                                        <th style="width: 10%">状态</th>
                                        <th style="width: 15%">操作</th>
                                    </tr>
                                </thead>
                                <tbody id="complaintRecordsBody">
                                    <!-- 数据将通过JavaScript动态加载 -->
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 申请详情模态框 -->
    <div class="modal fade ios-style" id="applicationDetailModal" tabindex="-1" aria-labelledby="applicationDetailModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg-custom">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="applicationDetailModalLabel">供应商申请详情</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body overflow-auto">
                    <div class="row mb-4">
                        <div class="col-md-8">
                            <div class="detail-grid">
                                <div class="detail-item">
                                    <div class="detail-label">供应商名称</div>
                                    <div class="detail-value" id="appDetail-providerName">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">企业类型</div>
                                    <div class="detail-value" id="appDetail-companyType">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">经理姓名</div>
                                    <div class="detail-value" id="appDetail-managerName">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">经理电话</div>
                                    <div class="detail-value" id="appDetail-managerPhone">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">注册资本</div>
                                    <div class="detail-value" id="appDetail-registeredCapital">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">员工数量</div>
                                    <div class="detail-value" id="appDetail-employeeCount">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">成立年份</div>
                                    <div class="detail-value" id="appDetail-establishedYear">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">申请状态</div>
                                    <div class="detail-value" id="appDetail-status">-</div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card h-100">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0">企业简介</h6>
                                </div>
                                <div class="card-body">
                                    <div class="detail-value description" id="appDetail-profile">-</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card mt-4" id="appDetail-reviewContainer" style="display: none;">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">审批意见</h6>
                        </div>
                        <div class="card-body">
                            <div class="detail-value description" id="appDetail-reviewDescription">-</div>
                        </div>
                    </div>

                    <div class="card mt-4" id="appDetail-reviewFormContainer">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">审批表单</h6>
                        </div>
                        <div class="card-body">
                            <form id="appReviewForm">
                                <div class="row mb-3">
                                    <div class="col-md-6">
                                        <label for="appApprovalStatus" class="form-label">处理意见: <span class="text-danger">*</span></label>
                                        <select class="form-select" id="appApprovalStatus" required>
                                            <option value="" selected disabled>请选择处理意见</option>
                                            <option value="true">审批通过</option>
                                            <option value="false">拒绝申请</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="appInitialRating" class="form-label">初始评分: <span class="text-danger">*</span></label>
                                        <input type="number" class="form-control" id="appInitialRating" min="0" max="5" value="3" required>
                                        <small class="text-muted">0-5分，默认3分</small>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label for="appDescription" class="form-label">审核意见: <span class="text-danger">*</span></label>
                                    <textarea class="form-control" id="appDescription" rows="3" required></textarea>
                                </div>
                                <input type="hidden" id="appApplicationId" value="">
                                <div class="text-end">
                                    <button type="button" class="btn btn-primary" onclick="submitApplicationReview()">提交审批</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 投诉详情模态框 -->
    <div class="modal fade ios-style" id="complaintDetailModal" tabindex="-1" aria-labelledby="complaintDetailModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg-custom">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="complaintDetailModalLabel">供应商投诉详情</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body overflow-auto">
                    <div class="row mb-4">
                        <div class="col-md-8">
                            <div class="detail-grid">
                                <div class="detail-item">
                                    <div class="detail-label">供应商名称</div>
                                    <div class="detail-value" id="compDetail-providerName">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">问题材料</div>
                                    <div class="detail-value" id="compDetail-defectiveMaterial">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">投诉人工号</div>
                                    <div class="detail-value" id="compDetail-complainant">-</div>
                                </div>
                                <div class="detail-item">
                                    <div class="detail-label">投诉状态</div>
                                    <div class="detail-value" id="compDetail-status">-</div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card h-100">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0">投诉描述</h6>
                                </div>
                                <div class="card-body">
                                    <div class="detail-value description" id="compDetail-description">-</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card mt-4" id="compDetail-reviewContainer" style="display: none;">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">审批意见</h6>
                        </div>
                        <div class="card-body">
                            <div class="detail-value description" id="compDetail-reviewDescription">-</div>
                        </div>
                    </div>

                    <div class="card mt-4" id="compDetail-reviewFormContainer">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">审批表单</h6>
                        </div>
                        <div class="card-body">
                            <form id="compReviewForm">
                                <div class="row mb-3">
                                    <div class="col-md-6">
                                        <label for="compApprovalStatus" class="form-label">处理意见: <span class="text-danger">*</span></label>
                                        <select class="form-select" id="compApprovalStatus" required>
                                            <option value="" selected disabled>请选择处理意见</option>
                                            <option value="true">审核通过</option>
                                            <option value="false">拒绝投诉</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="compRatingDeduction" class="form-label">供应商评级扣分: <span class="text-danger">*</span></label>
                                        <input type="number" class="form-control" id="compRatingDeduction" min="0" max="5" value="1" required>
                                        <small class="text-muted">0-5分，默认1分</small>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label for="compReviewDescription" class="form-label">审核意见: <span class="text-danger">*</span></label>
                                    <textarea class="form-control" id="compReviewDescription" rows="3" required></textarea>
                                </div>
                                <input type="hidden" id="compComplaintId" value="">
                                <div class="text-end">
                                    <button type="button" class="btn btn-primary" onclick="submitComplaintReview()">提交审核</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <script src="../../assets/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var menuItems = document.querySelectorAll('.menu-item > a');
            menuItems.forEach(function (item) {
                item.addEventListener('click', function (e) {
                    var parent = item.parentElement;
                    document.querySelectorAll('.menu-item').forEach(function (el) {
                        if (el !== parent) el.classList.remove('active');
                    });
                    parent.classList.toggle('active');
                    e.preventDefault();
                });
            });

            // 添加首页链接的点击事件
            document.querySelector('.menu-item:first-child > a').addEventListener('click', function(e) {
                e.preventDefault(); // 阻止默认行为
                window.location.href = '../homepage/administrator_home.html'; // 跳转到管理员首页
            });

            // 默认展开供应商管理菜单
            const providerMenuItem = document.querySelector('.menu-item:nth-child(6)');
            if (providerMenuItem) {
                providerMenuItem.classList.add('active');
            }
            // 设置当前页面对应的菜单项为选中状态
            const currentPath = window.location.pathname;
            const filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
            document.querySelectorAll('.submenu li a').forEach(link => {
                if (link.getAttribute('href') === filename) {
                    link.classList.add('active');
                }
            });
            // 加载供应商数据
            loadProviders();
        });

        async function loadProviders() {
            try {
                const response = await fetch('http://localhost:8080/api/providers');
                const data = await response.json();
                displayProviders(data);
            } catch (error) {
                console.error('加载供应商数据失败:', error);
                alert('加载供应商数据失败，请确保后端服务正在运行');
            }
        }

        function displayProviders(providers) {
            const tbody = document.getElementById('providerTableBody');
            tbody.innerHTML = '';
            
            // 添加数据验证，确保providers是数组
            if (!Array.isArray(providers)) {
                console.error('接收到的数据不是数组格式:', providers);
                tbody.innerHTML = '<tr><td colspan="10" class="text-center">无法加载数据，请检查后端服务</td></tr>';
                return;
            }
            
            if (providers.length === 0) {
                tbody.innerHTML = '<tr><td colspan="10" class="text-center">暂无供应商数据</td></tr>';
                return;
            }
            
            // 原有的遍历逻辑
            providers.forEach(provider => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${provider.providerName}</td>
                    <td>${provider.factoryAddress || ''}</td>
                    <td>${provider.managerName || ''}</td>
                    <td>${provider.managerPhone || ''}</td>
                    <td>${provider.companyType || ''}</td>
                    <td>${provider.registeredCapital || 0}</td>
                    <td>${provider.employeeCount || 0}</td>
                    <td>${provider.establishedYear || ''}</td>
                    <td>${provider.providerRating || 0}</td>
                    <td>
                        <div class="action-buttons">
                            <button class="btn btn-sm btn-info" onclick="editProvider('${provider.providerName}')">编辑</button>
                            <button class="btn btn-sm btn-danger" onclick="deleteProvider('${provider.providerName}')">删除</button>
                            <button class="btn btn-sm btn-secondary" onclick="viewProviderDetail('${provider.providerName}')">详情</button>
                        </div>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }

        function searchProviders() {
            const searchParams = {
                providerName: document.getElementById('searchProviderName').value.toLowerCase(),
                companyType: document.getElementById('searchCompanyType').value,
                managerName: document.getElementById('searchManagerName').value.toLowerCase(),
                providerRating: document.getElementById('searchProviderRating').value
            };
            const tbody = document.getElementById('providerTableBody');
            const rows = tbody.getElementsByTagName('tr');
            for (let row of rows) {
                const cells = row.getElementsByTagName('td');
                const rowData = {
                    providerName: cells[0].textContent.toLowerCase(),
                    companyType: cells[4].textContent,
                    managerName: cells[2].textContent.toLowerCase(),
                    providerRating: cells[8].textContent
                };
                let visible = true;
                for (const [key, value] of Object.entries(searchParams)) {
                    if (value && key === 'providerRating') {
                        if (rowData[key] != value) {
                            visible = false;
                            break;
                        }
                    } else if (value && !rowData[key].includes(value)) {
                        visible = false;
                        break;
                    }
                }
                row.style.display = visible ? '' : 'none';
            }
        }

        function resetSearch() {
            document.getElementById('searchProviderName').value = '';
            document.getElementById('searchCompanyType').value = '';
            document.getElementById('searchManagerName').value = '';
            document.getElementById('searchProviderRating').value = '';
            const tbody = document.getElementById('providerTableBody');
            const rows = tbody.getElementsByTagName('tr');
            for (let row of rows) {
                row.style.display = '';
            }
        }

        function addProvider() {
            window.location.href = 'provider_edit.html';
        }
        
        function editProvider(providerName) {
            window.location.href = `provider_edit.html?name=${encodeURIComponent(providerName)}`;
        }

        function viewProviderDetail(providerName) {
            getProviderByName(providerName).then(provider => {
                if (provider) {
                    document.getElementById('detailProviderName').textContent = provider.providerName || '-';
                    document.getElementById('detailFactoryAddress').textContent = provider.factoryAddress || '-';
                    document.getElementById('detailManagerName').textContent = provider.managerName || '-';
                    document.getElementById('detailManagerPhone').textContent = provider.managerPhone || '-';
                    document.getElementById('detailCompanyType').textContent = provider.companyType || '-';
                    document.getElementById('detailRegisteredCapital').textContent = provider.registeredCapital || '-';
                    document.getElementById('detailEmployeeCount').textContent = provider.employeeCount || '-';
                    document.getElementById('detailEstablishedYear').textContent = provider.establishedYear || '-';
                    document.getElementById('detailProviderRating').textContent = provider.providerRating || '-';
                    document.getElementById('detailProfile').textContent = provider.profile || '-';
                    showProviderDetailModal();
                }
            });
        }

        // 显示iOS风格模态框
        function showProviderDetailModal() {
            const modal = document.getElementById('providerDetailModal');
            modal.classList.add('show');
            modal.onclick = function (e) {
                if (e.target === modal) {
                    closeProviderDetailModal();
                }
            };
        }

        // 关闭iOS风格模态框
        function closeProviderDetailModal() {
            const modal = document.getElementById('providerDetailModal');
            modal.classList.remove('show');
        }

        // ESC键关闭模态框
        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape') {
                closeProviderDetailModal();
            }
        });
        
        async function getProviderByName(providerName) {
            try {
                const response = await fetch(`http://localhost:8080/api/providers/${encodeURIComponent(providerName)}`);
                const provider = await response.json();
                return provider;
            } catch (error) {
                console.error('获取供应商详情失败:', error);
                return null;
            }
        }
        
        async function deleteProvider(providerName) {
            if (confirm('确定要删除这个供应商吗？')) {
                try {
                    const response = await fetch(`http://localhost:8080/api/providers/${encodeURIComponent(providerName)}`, {
                        method: 'DELETE'
                    });
                    if (response.ok) {
                        const result = await response.json();
                        alert(result.message);
                        loadProviders();
                    } else {
                        const error = await response.json();
                        alert(error.message || '删除失败');
                    }
                } catch (error) {
                    console.error('删除供应商时出错:', error);
                    alert('删除供应商时发生错误，请稍后重试');
                }
            }
        }
        
        function logout() {
            localStorage.removeItem('userInfo');
            window.location.href = '../../../login.html';
        }

        // 显示申请记录模态框
        function showApplicationRecordsModal() {
            loadApplicationRecords();
            const modal = new bootstrap.Modal(document.getElementById('applicationRecordsModal'));
            modal.show();
        }

        // 显示投诉记录模态框
        function showComplaintRecordsModal() {
            loadComplaintRecords();
            const modal = new bootstrap.Modal(document.getElementById('complaintRecordsModal'));
            modal.show();
        }

        // 加载供应商申请记录
        async function loadApplicationRecords() {
            try {
                const response = await fetch('http://localhost:8080/api/provider/application/getApplications');
                if (!response.ok) {
                    throw new Error(`HTTP错误！状态: ${response.status}`);
                }
                const applications = await response.json();
                displayApplicationRecords(applications);
            } catch (error) {
                console.error('加载供应商申请记录失败:', error);
                document.getElementById('applicationRecordsBody').innerHTML = '<tr><td colspan="6" class="text-center">加载失败，请稍后重试</td></tr>';
            }
        }

        // 显示供应商申请记录
        function displayApplicationRecords(applications) {
            const tbody = document.getElementById('applicationRecordsBody');
            tbody.innerHTML = '';
            
            if (!Array.isArray(applications) || applications.length === 0) {
                tbody.innerHTML = '<tr><td colspan="6" class="text-center">暂无申请记录</td></tr>';
                return;
            }
            
            applications.forEach(app => {
                let statusText = '';
                let statusClass = '';
                
                if (app.status) {
                    statusText = app.isApproved ? '已批准' : '已驳回';
                    statusClass = app.isApproved ? 'status-approved' : 'status-rejected';
                } else {
                    statusText = '待审核';
                    statusClass = 'status-pending';
                }
                
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${app.providerName || '-'}</td>
                    <td>${app.companyType || '-'}</td>
                    <td>${app.managerName || '-'}</td>
                    <td>${app.applicantJobNumber || '-'}</td>
                    <td><span class="status-badge ${statusClass}">${statusText}</span></td>
                    <td>
                        <button class="btn btn-sm btn-info" onclick="viewApplicationDetail(${app.applicationId})">详情</button>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }

        // 搜索供应商申请
        function searchApplications() {
            const providerName = document.getElementById('appSearchProviderName').value.toLowerCase();
            const status = document.getElementById('appSearchStatus').value;
            
            const rows = document.getElementById('applicationRecordsBody').getElementsByTagName('tr');
            for (let row of rows) {
                const cells = row.getElementsByTagName('td');
                if (cells.length === 0) continue;
                
                const rowProviderName = cells[0].textContent.toLowerCase();
                const statusSpan = cells[4].querySelector('.status-badge');
                const rowStatus = statusSpan ? statusSpan.textContent : '';
                
                let matchProviderName = true;
                let matchStatus = true;
                
                if (providerName && !rowProviderName.includes(providerName)) {
                    matchProviderName = false;
                }
                
                if (status) {
                    if (status === 'pending' && rowStatus !== '待审核') matchStatus = false;
                    if (status === 'approved' && rowStatus !== '已批准') matchStatus = false;
                    if (status === 'rejected' && rowStatus !== '已驳回') matchStatus = false;
                }
                
                row.style.display = (matchProviderName && matchStatus) ? '' : 'none';
            }
        }

        // 重置供应商申请搜索
        function resetApplicationSearch() {
            document.getElementById('appSearchProviderName').value = '';
            document.getElementById('appSearchStatus').value = '';
            
            const rows = document.getElementById('applicationRecordsBody').getElementsByTagName('tr');
            for (let row of rows) {
                row.style.display = '';
            }
        }

        // 查看申请详情
        async function viewApplicationDetail(applicationId) {
            try {
                // 获取申请详情
                const appResponse = await fetch(`http://localhost:8080/api/provider/application/getApplicationById/${applicationId}`);
                if (!appResponse.ok) {
                    throw new Error(`HTTP错误！状态: ${appResponse.status}`);
                }
                const application = await appResponse.json();
                
                // 填充申请详情
                document.getElementById('appDetail-providerName').textContent = application.providerName || '-';
                document.getElementById('appDetail-companyType').textContent = application.companyType || '-';
                document.getElementById('appDetail-managerName').textContent = application.managerName || '-';
                document.getElementById('appDetail-managerPhone').textContent = application.managerPhone || '-';
                document.getElementById('appDetail-registeredCapital').textContent = application.registeredCapital || '-';
                document.getElementById('appDetail-employeeCount').textContent = application.employeeCount || '-';
                document.getElementById('appDetail-establishedYear').textContent = application.establishedYear || '-';
                document.getElementById('appDetail-profile').textContent = application.profile || '-';
                
                // 设置申请状态
                let statusText = '';
                if (application.status) {
                    statusText = application.isApproved ? '已批准' : '已驳回';
                } else {
                    statusText = '待审核';
                }
                document.getElementById('appDetail-status').textContent = statusText;
                
                // 存储申请ID
                document.getElementById('appApplicationId').value = applicationId;
                
                // 如果已经审核过，获取并显示审核信息
                if (application.status) {
                    try {
                        const reviewResponse = await fetch(`http://localhost:8080/api/provider/application/getReviewById/${applicationId}`);
                        if (reviewResponse.ok) {
                            const review = await reviewResponse.json();
                            document.getElementById('appDetail-reviewDescription').textContent = review.description || '-';
                            document.getElementById('appDetail-reviewContainer').style.display = 'block';
                            document.getElementById('appDetail-reviewFormContainer').style.display = 'none';
                        }
                    } catch (error) {
                        console.error('获取审核信息失败:', error);
                    }
                } else {
                    document.getElementById('appDetail-reviewContainer').style.display = 'none';
                    document.getElementById('appDetail-reviewFormContainer').style.display = 'block';
                }
                
                // 显示详情模态框
                const modal = new bootstrap.Modal(document.getElementById('applicationDetailModal'));
                modal.show();
                
            } catch (error) {
                console.error('获取申请详情失败:', error);
                alert('获取申请详情失败，请稍后重试');
            }
        }

        // 提交申请审核
        async function submitApplicationReview() {
            const applicationId = document.getElementById('appApplicationId').value;
            const isApproved = document.getElementById('appApprovalStatus').value;
            const initialRating = document.getElementById('appInitialRating').value;
            const description = document.getElementById('appDescription').value;
            
            if (!isApproved || !description) {
                alert('请填写完整的审核信息');
                return;
            }
            
            // 先获取申请详情
            let application;
            try {
                const appResponse = await fetch(`http://localhost:8080/api/provider/application/getApplicationById/${applicationId}`);
                if (!appResponse.ok) {
                    throw new Error(`获取申请详情失败: ${appResponse.status}`);
                }
                application = await appResponse.json();
            } catch (error) {
                console.error('获取申请详情失败:', error);
                alert('获取申请详情失败，请稍后重试');
                return;
            }
            
            // 更新申请状态
            try {
                const updateData = {
                    ...application,
                    status: true,
                    isApproved: isApproved === 'true'
                };
                
                const appUpdateResponse = await fetch('http://localhost:8080/api/provider/application/update', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(updateData)
                });
                
                if (!appUpdateResponse.ok) {
                    throw new Error(`更新申请状态失败: ${appUpdateResponse.status}`);
                }
            } catch (error) {
                console.error('更新申请状态失败:', error);
                alert('更新申请状态失败，请稍后重试');
                return;
            }
            
            // 然后提交审核结果
            const reviewData = {
                applicationId: parseInt(applicationId),
                isApproved: isApproved === 'true',
                description: description,
                initialRating: parseInt(initialRating)
            };
            
            try {
                const response = await fetch('http://localhost:8080/api/provider/application/review', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(reviewData)
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP错误！状态: ${response.status}`);
                }
                
                alert('审核成功');
                loadApplicationRecords();
                const modal = bootstrap.Modal.getInstance(document.getElementById('applicationDetailModal'));
                modal.hide();
                
            } catch (error) {
                console.error('提交审核失败:', error);
                alert('提交审核失败，请稍后重试');
            }
        }

        // 加载供应商投诉记录
        async function loadComplaintRecords() {
            try {
                const response = await fetch('http://localhost:8080/api/provider/complaints/getComplaint');
                if (!response.ok) {
                    throw new Error(`HTTP错误！状态: ${response.status}`);
                }
                const complaints = await response.json();
                displayComplaintRecords(complaints);
            } catch (error) {
                console.error('加载供应商投诉记录失败:', error);
                document.getElementById('complaintRecordsBody').innerHTML = '<tr><td colspan="6" class="text-center">加载失败，请稍后重试</td></tr>';
            }
        }

        // 显示供应商投诉记录
        function displayComplaintRecords(complaints) {
            const tbody = document.getElementById('complaintRecordsBody');
            tbody.innerHTML = '';
            
            if (!Array.isArray(complaints) || complaints.length === 0) {
                tbody.innerHTML = '<tr><td colspan="6" class="text-center">暂无投诉记录</td></tr>';
                return;
            }
            
            complaints.forEach(complaint => {
                let statusText = '';
                let statusClass = '';
                
                if (complaint.status) {
                    statusText = complaint.isApproved ? '已批准' : '已驳回';
                    statusClass = complaint.isApproved ? 'status-approved' : 'status-rejected';
                } else {
                    statusText = '待审核';
                    statusClass = 'status-pending';
                }
                
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${complaint.providerName || '-'}</td>
                    <td>${complaint.defectiveMaterial || '-'}</td>
                    <td class="description-cell">${complaint.description || '-'}</td>
                    <td>${complaint.complainantJobNumber || '-'}</td>
                    <td><span class="status-badge ${statusClass}">${statusText}</span></td>
                    <td>
                        <button class="btn btn-sm btn-info" onclick="viewComplaintDetail(${complaint.complaintId})">详情</button>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }

        // 搜索供应商投诉
        function searchComplaints() {
            const providerName = document.getElementById('compSearchProviderName').value.toLowerCase();
            const status = document.getElementById('compSearchStatus').value;
            
            const rows = document.getElementById('complaintRecordsBody').getElementsByTagName('tr');
            for (let row of rows) {
                const cells = row.getElementsByTagName('td');
                if (cells.length === 0) continue;
                
                const rowProviderName = cells[0].textContent.toLowerCase();
                const statusSpan = cells[4].querySelector('.status-badge');
                const rowStatus = statusSpan ? statusSpan.textContent : '';
                
                let matchProviderName = true;
                let matchStatus = true;
                
                if (providerName && !rowProviderName.includes(providerName)) {
                    matchProviderName = false;
                }
                
                if (status) {
                    if (status === 'pending' && rowStatus !== '待审核') matchStatus = false;
                    if (status === 'approved' && rowStatus !== '已批准') matchStatus = false;
                    if (status === 'rejected' && rowStatus !== '已驳回') matchStatus = false;
                }
                
                row.style.display = (matchProviderName && matchStatus) ? '' : 'none';
            }
        }

        // 重置供应商投诉搜索
        function resetComplaintSearch() {
            document.getElementById('compSearchProviderName').value = '';
            document.getElementById('compSearchStatus').value = '';
            
            const rows = document.getElementById('complaintRecordsBody').getElementsByTagName('tr');
            for (let row of rows) {
                row.style.display = '';
            }
        }

        // 查看投诉详情
        async function viewComplaintDetail(complaintId) {
            try {
                // 获取投诉详情
                const compResponse = await fetch(`http://localhost:8080/api/provider/complaints/getComplaintById/${complaintId}`);
                if (!compResponse.ok) {
                    throw new Error(`HTTP错误！状态: ${compResponse.status}`);
                }
                const complaint = await compResponse.json();
                
                // 填充投诉详情
                document.getElementById('compDetail-providerName').textContent = complaint.providerName || '-';
                document.getElementById('compDetail-defectiveMaterial').textContent = complaint.defectiveMaterial || '-';
                document.getElementById('compDetail-complainant').textContent = complaint.complainantJobNumber || '-';
                document.getElementById('compDetail-description').textContent = complaint.description || '-';
                
                // 设置投诉状态
                let statusText = '';
                if (complaint.status) {
                    statusText = complaint.isApproved ? '已批准' : '已驳回';
                } else {
                    statusText = '待审核';
                }
                document.getElementById('compDetail-status').textContent = statusText;
                
                // 存储投诉ID
                document.getElementById('compComplaintId').value = complaintId;
                
                // 如果已经审核过，获取并显示审核信息
                if (complaint.status) {
                    try {
                        const reviewResponse = await fetch(`http://localhost:8080/api/provider/complaints/getReviewById/${complaintId}`);
                        if (reviewResponse.ok) {
                            const review = await reviewResponse.json();
                            document.getElementById('compDetail-reviewDescription').textContent = review.description || '-';
                            document.getElementById('compDetail-reviewContainer').style.display = 'block';
                            document.getElementById('compDetail-reviewFormContainer').style.display = 'none';
                        }
                    } catch (error) {
                        console.error('获取审核信息失败:', error);
                    }
                } else {
                    document.getElementById('compDetail-reviewContainer').style.display = 'none';
                    document.getElementById('compDetail-reviewFormContainer').style.display = 'block';
                }
                
                // 显示详情模态框
                const modal = new bootstrap.Modal(document.getElementById('complaintDetailModal'));
                modal.show();
                
            } catch (error) {
                console.error('获取投诉详情失败:', error);
                alert('获取投诉详情失败，请稍后重试');
            }
        }

        // 提交投诉审核
        async function submitComplaintReview() {
            const complaintId = document.getElementById('compComplaintId').value;
            const isApproved = document.getElementById('compApprovalStatus').value;
            const ratingDeduction = document.getElementById('compRatingDeduction').value;
            const description = document.getElementById('compReviewDescription').value;
            
            if (!isApproved || !description) {
                alert('请填写完整的审核信息');
                return;
            }
            
            // 先获取投诉详情
            let complaint;
            try {
                const compResponse = await fetch(`http://localhost:8080/api/provider/complaints/getComplaintById/${complaintId}`);
                if (!compResponse.ok) {
                    throw new Error(`获取投诉详情失败: ${compResponse.status}`);
                }
                complaint = await compResponse.json();
            } catch (error) {
                console.error('获取投诉详情失败:', error);
                alert('获取投诉详情失败，请稍后重试');
                return;
            }
            
            // 更新投诉状态
            try {
                const updateData = {
                    ...complaint,
                    status: true,
                    isApproved: isApproved === 'true'
                };
                
                const compUpdateResponse = await fetch('http://localhost:8080/api/provider/complaints/update', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(updateData)
                });
                
                if (!compUpdateResponse.ok) {
                    throw new Error(`更新投诉状态失败: ${compUpdateResponse.status}`);
                }
            } catch (error) {
                console.error('更新投诉状态失败:', error);
                alert('更新投诉状态失败，请稍后重试');
                return;
            }
            
            // 然后提交审核结果
            const reviewData = {
                complaintId: parseInt(complaintId),
                isApproved: isApproved === 'true',
                description: description,
                ratingDeduction: parseInt(ratingDeduction)
            };
            
            try {
                const response = await fetch('http://localhost:8080/api/provider/complaints/review', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(reviewData)
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP错误！状态: ${response.status}`);
                }
                
                alert('审核成功');
                loadComplaintRecords();
                const modal = bootstrap.Modal.getInstance(document.getElementById('complaintDetailModal'));
                modal.hide();
                
            } catch (error) {
                console.error('提交审核失败:', error);
                alert('提交审核失败，请稍后重试');
            }
        }
    </script>
</body>
</html>